clear
discard
set more off

adopath ++ "./ado/"

******************
*** Parameters ***
******************

local file_name_consensus "./input/consensus_dataset.csv"  // Data file
local file_name_individual "./input/individual_dataset.csv" // Data file
local output_folder "./output/reduced_form" // Output directory
local alpha = 0.05 // Significance level
local L_NW = 5 // Number of lags for calculating Newey-West standard errors

*****************************
*** Import CONSENSUS data ***
*****************************

import delimited using `file_name_consensus', case(preserve)
rename DATE Date

gen qtr = quarterly(Date,"YQ")
format qtr %tq
tsset qtr // Set to time series

**************************
*** Generate variables ***
**************************

gen Efor_Step2 = Realiz1 - SPFfor_Step2
gen Rfor_Step2 = SPFfor_Step2 - SPFfor_Step3
gen L1_Efor_Step2 = l1.Efor_Step2
gen L1_Realiz1 = l1.Realiz1

*******************
*** Regressions ***
*******************

* Clean up
shell rm -r `output_folder'
shell mkdir -p ./output
shell mkdir `output_folder'

* Ensure common sample
drop if (missing(Efor_Step2) | missing(Rfor_Step2) | missing(L1_Efor_Step2) | missing(L1_Realiz1))

* Regressions
newey Efor_Step2 Rfor_Step2, lag(`L_NW')
matrix CG_coefs_cons = e(b)
matrix CG_cov_cons = e(V)

newey Efor_Step2 L1_Efor_Step2, lag(`L_NW')
matrix autocorr_coefs_cons = e(b)
matrix autocorr_cov_cons = e(V)

newey Efor_Step2 L1_Realiz1, lag(`L_NW')
matrix extrap_coefs_cons = e(b)
matrix extrap_cov_cons = e(V)

* Save output
mat2txt, matrix(CG_coefs_cons) saving(`output_folder'/ex_CG_coefs_consensus.txt)
mat2txt, matrix(CG_cov_cons) saving(`output_folder'/ex_CG_cov_consensus.txt)
matrix N_cons = e(N)

mat2txt, matrix(autocorr_coefs_cons) saving(`output_folder'/ex_autocorr_coefs_consensus.txt)
mat2txt, matrix(autocorr_cov_cons) saving(`output_folder'/ex_autocorr_cov_consensus.txt)

mat2txt, matrix(extrap_coefs_cons) saving(`output_folder'/ex_extrap_coefs_consensus.txt)
mat2txt, matrix(extrap_cov_cons) saving(`output_folder'/ex_extrap_cov_consensus.txt)

mat2txt, matrix(N_cons) saving(`output_folder'/N_cons.txt)

clear

******************************
*** Import INDIVIDUAL data ***
******************************

import delimited using `file_name_individual', case(preserve)

gen qtr = quarterly(DATE,"YQ")
format qtr %tq
xtset ID qtr // Set to panel data

**************************
*** Generate variables ***
**************************

gen Efor_Step2 = Realiz1 - SPFfor_Step2
gen Rfor_Step2 = SPFfor_Step2 - SPFfor_Step3
gen L1_Efor_Step2 = l1.Efor_Step2
gen L1_Realiz1 = l1.Realiz1

*******************
*** Regressions ***
*******************

* Ensure common sample
drop if (missing(Efor_Step2) | missing(Rfor_Step2) | missing(L1_Efor_Step2) | missing(L1_Realiz1))

* Regressions
reghdfe Efor_Step2 Rfor_Step2, absorb(ID) vce(cluster ID DATE)
matrix CG_coefs_micro = e(b)
matrix CG_cov_micro = e(V)
matrix N_micro = e(N)

reghdfe Efor_Step2 L1_Efor_Step2, absorb(ID) vce(cluster ID DATE)
matrix autocorr_coefs_micro = e(b)
matrix autocorr_cov_micro = e(V)

reghdfe Efor_Step2 L1_Realiz1, absorb(ID) vce(cluster ID DATE)
matrix extrap_coefs_micro = e(b)
matrix extrap_cov_micro = e(V)


* Save output
mat2txt, matrix(CG_coefs_micro) saving(`output_folder'/ex_CG_coefs_micro.txt)
mat2txt, matrix(CG_cov_micro) saving(`output_folder'/ex_CG_cov_micro.txt)

mat2txt, matrix(autocorr_coefs_micro) saving(`output_folder'/ex_autocorr_coefs_micro.txt)
mat2txt, matrix(autocorr_cov_micro) saving(`output_folder'/ex_autocorr_cov_micro.txt)

mat2txt, matrix(extrap_coefs_micro) saving(`output_folder'/ex_extrap_coefs_micro.txt)
mat2txt, matrix(extrap_cov_micro) saving(`output_folder'/ex_extrap_cov_micro.txt)

mat2txt, matrix(N_micro) saving(`output_folder'/N_micro.txt)


